package com.wsm.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 *
 */
public class DateUtil {

    public static final String yyyyMMdd = "yyyyMMdd";

    public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";

    public static final String yyyyMMddHHmm = "yyyyMMddHHmm";

    public static final String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
    /**
     * 格式2011年04月20日
     */
    public static final String yyyyMMYddR = "yyyy年MM月dd日";

    public static final String yyyyMMY = "yyyy年MM月";

    /**
     * 格式11年04月20日
     */
    public static final String yyMMYddR = "yy年MM月dd日";
    /**
     * 格式2011年4月20日4时23分
     */
    public static final String yyyyNMMYddHHHmm = "yyyy年MM月dd号 HH时mm分";

    public static final String MMYddHHHmm = "MM月dd日 HH:mm";

    public static final String yyyyMMddSplit = "yyyy-MM-dd";

    public static final String yyyyMMddSplitTime = "yyyy-MM-dd HH:mm";

    public static final String yyyyMMddSplitTime2 = "yyyy-MM-dd HH:mm:ss";

    public static final String MMddSplitTime = "MM-dd HH:mm";

    public static final String HHmm = "HH:mm";

    public static final String HHmmss = "HH:mm:ss";

    public static final String HHmmssNoComma = "HHmmss";

    public static final String yyyyNMMYddRHHmm = "yyyy年MM月dd日 HH:mm";

    /*
    /**
     * date类型转指定日期字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String parseDateToString(Date date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String dateStr = null;
        try {
            dateStr = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * 日期字符串转date类型
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date parseStringToDate(String dateStr, String format) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date parseDate(Date date, String format) {
        String dateStr = parseDateToString(date, format);
        return parseStringToDate(dateStr, format);
    }

    /**
     * 日期增加天数
     *
     * @param date
     * @param day  天数
     * @return
     */
    public static Date addDayDate(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 今天+N天
        c.add(Calendar.DAY_OF_MONTH, day);
        Date time = c.getTime();
        return time;
    }

    /**
     * 日期增加天数
     *
     * @param date
     * @param day    天数
     * @param format
     * @return
     */
    public static String dateAddDay(Date date, int day, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 今天+N天
        c.add(Calendar.DAY_OF_MONTH, day);
        return formatter.format(c.getTime());
    }

    /**
     * 日期增加分钟数
     *
     * @param date
     * @param min    分钟数
     * @param format
     * @return
     */
    public static Date dateAddMin(Date date, int min, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 今天+N天
        c.add(Calendar.MINUTE, min);
        return c.getTime();
    }

    /**
     * 日期增加秒数
     *
     * @param date
     * @param second 秒数
     * @return
     */
    public static Date dateAddSecond(Date date, int second) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 今天+N天
        c.add(Calendar.SECOND, second);
        return c.getTime();
    }

    /**
     * 日期增加秒数
     *
     * @param date
     * @param hour 秒数
     * @return
     */
    public static Date dateAddHour(Date date, int hour) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 今天+N天
        c.add(Calendar.HOUR, hour);
        return c.getTime();
    }

    /**
     * 日期区间天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDays(String startDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        /** 计算起止日期间的天数 */
        int range = 0;
        try {
            long to = sdf.parse(endDate).getTime();
            long from = sdf.parse(startDate).getTime();
            range = (int) ((to - from) / (1000 * 60 * 60 * 24));
        } catch (Exception e) {
            e.printStackTrace();
            range = 0;
        }
        return range;
    }

    public static int getDays(Long startDate, Long endDate) {
        /** 计算起止日期间的天数 */
        return (int) ((endDate - startDate) / (1000 * 60 * 60 * 24));
    }


    public static String latest(Date latest) {
        if (latest == null) {
            return null;
        }
        Date currentTime = new Date();
        long newTime = currentTime.getTime() - latest.getTime();
        long days = newTime / (1000 * 60 * 60 * 24);
        if (days > 0) {
            return days + "天前";
        } else {
            long hours = (newTime - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            if (hours >= 1) {
                return hours + "小时前";
            } else {
                return "半小时前";
            }
        }
    }

    /**
     * 日期相差秒数
     *
     * @return
     */
    public static Integer dateInterval(Date beginDate, Date endDate) {
        Long begin = beginDate.getTime();
        Long end = endDate.getTime();
        return ((end.intValue() - begin.intValue()) / 1000);
    }

    /**
     * 根据开始时间和结束时间返回时间段内的时间集合
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
        if (beginDate != null && endDate != null) {
            List<Date> lDate = new ArrayList<Date>();
            // 把开始时间加入集合
            lDate.add(beginDate);
            Calendar cal = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            cal.setTime(beginDate);
            while (true) {
                // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                cal.add(Calendar.DAY_OF_MONTH, 1);
                // 测试此日期是否在指定日期之后
                if (endDate.after(cal.getTime())) {
                    lDate.add(cal.getTime());
                } else {
                    break;
                }
            }
            // 把结束时间加入集合
            lDate.add(endDate);
            return lDate;
        } else {
            return null;
        }
    }

    /**
     * 日期比较是否相等
     *
     * @return 相等返回 true 不等 false
     */
    public static Boolean compareDate(Date oneDate, String twoDate) {
        Boolean result = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String one = sdf.format(oneDate);
        if (one.equals(twoDate)) {
            result = true;
        }
        return result;
    }

    public static boolean isBetween(String check, String start, String end) {
        Date checkDate = parseStringToDate(check, yyyyMMdd);
        Date startDate = parseStringToDate(start, yyyyMMdd);
        Date endDate = parseStringToDate(end, yyyyMMdd);
        return startDate.getTime() <= checkDate.getTime() && checkDate.getTime() <= endDate.getTime();
    }

    public static boolean isBetween(String check, Date start, Date end) {
        Date checkDate = parseStringToDate(check, yyyyMMdd);
        long endTime = end.getTime();
        long checkDateTime = checkDate.getTime();
        long startTime = start.getTime();
        return endTime >= checkDateTime && checkDateTime >= startTime;
    }

    public static boolean isBetween(Date check, Date start, Date end) {
        long endTime = end.getTime();
        long checkDateTime = check.getTime();
        long startTime = start.getTime();
        return endTime >= checkDateTime && checkDateTime >= startTime;
    }

    public static boolean isBetween(Date check, String start, String end) {
        Date startDate = parseStringToDate(start, yyyyMMddSplitTime2);
        Date endDate = parseStringToDate(end, yyyyMMddSplitTime2);
        long time = startDate.getTime();
        long time1 = check.getTime();
        long time2 = endDate.getTime();
        return time <= time1 && time1 <= time2;
    }

    public static boolean isBetween(Long nowTime, Long startDate, Long endDate) {
        if (nowTime != null) {
            if (startDate == null) {
                if (endDate >= nowTime) {
                    return true;
                }
            }
            if (endDate == null) {
                if (nowTime >= startDate) {
                    return true;
                }
            }
            if (nowTime <= endDate && nowTime >= startDate) {
                return true;
            }
        }
        return false;
    }

    /**
     * 两个日期之间的所有日期列表
     * [20191128-20191102)
     * 20191128
     * 20191129
     * 20191130
     * 20191201
     *
     * @param checkIn  开
     * @param checkOut 闭
     * @return
     */
    public static List<String> region(String checkIn, String checkOut) {
        // 估算初始容量
        List<String> result = new ArrayList<>(getDays(checkIn, checkOut));

        SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd);
        try {
            Date start = sdf.parse(checkIn);
            Date end = sdf.parse(checkOut);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            while (tempStart.before(tempEnd) && !tempStart.equals(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;

    }

    public static List<String> region(Date checkIn, Date checkOut) {
        // 估算初始容量
        List<String> result = new ArrayList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(checkIn);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(checkOut);
        while (tempStart.before(tempEnd) && !tempStart.equals(tempEnd)) {
            result.add(parseDateToString(tempStart.getTime(), yyyyMMdd));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;

    }

    public static boolean isBetweenDate(Date startTime, Date endTime) {
        if (startTime == null) {
            return false;
        }
        Date now = new Date();
        return now.compareTo(startTime) >= 0 && now.compareTo(endTime) < 0;
    }

}
